﻿//高光模型
Shader "Demon/Demon_BlinnPhong/BlinnPhong"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_Specular("Specular 高光强度(X)高光扩散(Y)",vector) = (0,0,0,0)
		_SpecularColor("_SpecularColor",Color) = (1,1,1,1)
		_Color("Color",Color) = (1,1,1,1)
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
				float3 worldNormal : TEXCOORD1;
				float3 worldPos    : TEXCOORD2;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float4 _Specular;
			float4 _SpecularColor;
			float4 _Color;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				//本地坐标转世界坐标
				o.worldPos = mul(unity_ObjectToWorld,v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				//高光 = 直射光颜色 * 反射光颜色 * pow(max(0, dot(法线方向, 视野与光线中间向量)), 光泽度(gloss))
				//dot(法线方向, 视野与光线中间向量)  -> dot(N,H)  H = normalize(V+L)
				float3 L = normalize(_WorldSpaceLightPos0.xyz);  //入射光方向
				float3 V = normalize(_WorldSpaceCameraPos.xyz - i.worldPos); // 观察方向
				float3 H = normalize(L+V);  // 求半角向量
				float3 N = normalize(i.worldNormal); // 法线
				fixed NdotH = max(0,dot(H,N)*0.5+0.5);

				float3 lightColor = _LightColor0.rgb;  //光照颜色
				fixed3 specular;
				specular = _Specular.x * pow(NdotH,_Specular.y);
				
				specular = lightColor * _Color.rgb * specular;
				return fixed4(specular,1);
			}
			ENDCG
		}
	}
}
